import path from 'path';
import { defineConfig, loadEnv } from 'vite';
import vitePluginImportus from 'vite-plugin-importus';
import react from '@vitejs/plugin-react';
import { createHtmlPlugin } from 'vite-plugin-html';
import { visualizer } from 'rollup-plugin-visualizer';

const NODE_ENV = process.env.NODE_ENV || 'development';
const env = loadEnv(NODE_ENV, __dirname);

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        react(),
        vitePluginImportus([
            {
                libraryName: 'antd',
                libraryDirectory: 'es',
                style: (name) => {
                    if (name === 'antd/es/theme') return false;
                    return '/style/index.js';
                },
            },
            {
                libraryName: '@ant-design/icons',
                camel2DashComponentName: false,
                customName: (name) => `@ant-design/icons/${name}`,
            },
            {
                libraryName: 'lodash',
                camel2DashComponentName: false,
                customName: (name) => `lodash/${name}`,
            },
        ]),
        env.VITE_BUILD_REPORT === 'true' && visualizer({
            emitFile: false,
            file: 'stats.html',
            open: true,
        }),
        createHtmlPlugin({
            minify: true,
            entry: '/src/main.jsx',
            template: 'index.html',
            inject: {
                data: {
                    title: env.VITE_APP_NAME,
                    headHtml: [

                    ],
                    bodyHtml: [
                        '<div id="viewer-container" class="map"></div>',
                        '<div id="root"></div>',
                        '<script type="module" src="/src/main.jsx"></script>',
                    ],
                },
            },
        }),
    ],
    resolve: { alias: { '@': path.resolve(__dirname, 'src') } },
    server: {
        host: '0.0.0.0',
        port: 3000,
        proxy: {
            '/static': {
                target: 'http://10.8.89.243',
                rewrite: (url) => url.replace(/^\/static/, '/ftproot/static'),
            },
            '/cscgzf': {
                target: 'https://www.cscgzd.com',
                changeOrigin: true,
                configure: (proxy) => {
                    proxy.on('proxyReq', (req) => {
                        if (env.VITE_COOKIE) {
                            req.setHeader('Cookie', env.VITE_COOKIE);
                        }
                    });
                },
            },
            '/advise': { target: 'http://10.8.88.26:8888' },
            '/caseInfo': { target: 'http://10.8.88.26:8888' },
            '/ppx': {
                target: 'http://110.40.220.245:8083',
                changeOrigin: true,
            },
        },
    },
    build: {
        emptyOutDir: true,
        outDir: 'dist',
    },
});
